// pages/user/index.js
import { UserModel } from "../../models/user.js";
const userModel = new UserModel();
import {
  promisic
} from '../../util/common.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    user_info:null,
    searchIsShow:false,
    itemList: [
        {
          name: '成绩/证书查询',
        },
        {
          name: '软考报考指南',
        },
        {
            name: '考试安排最新通知',
        }
      ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '用户中心'
    });
  },
   getUserProfile(e){
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res.userInfo);
      }
    })
  },
  async login(options) {
    wx.showLoading({
      title: '登录中',
    });
    const res = await promisic(wx.getSetting)();
    var that = this;
    if (!res.authSetting['scope.userInfo']) {
      await promisic(wx.authorize)({scope: 'scope.userInfo'});
    }
    const userData=await promisic(wx.getUserProfile)({desc:'用于完善会员资料'});
    console.log(userData);
    const data = await promisic(wx.login)();
    const nickName=userData.userInfo.nickName;
    const userImg=userData.userInfo.avatarUrl;
    const userGender=userData.userInfo.gender;
    const user_info = await userModel.userLogin(data.code,nickName,userImg,userGender);
    wx.setStorageSync("web_user_info", user_info);
    that.setData({
      isLogin: true,
      user_info: user_info
    });

    wx.hideLoading();
  },
  //数据操作
  operType: function (options) {
    let type = options.currentTarget.dataset.type;
    if(type==1){
      //热门问题
      wx.navigateTo({
        url: "/pages/article/index",
      });
    }else if(type==2){
      //留言反馈
      wx.navigateTo({
        url: "/pages/feed_back/index",
      });
    }else if(type==3){
      //关于我们
      wx.navigateTo({
        url: "/pages/aboutus/index",
      });
    }else if(type==4){
      //设置信息
      wx.navigateTo({
        url: "/pages/setinfo/index",
      });
    }
  },
   //错题本
   errorBook:function(params) {
    wx.navigateTo({
        url: `/pages/error_book/index?courseId=${this.data.user_info.selTypeId}`
    });
  },
  //模考记录
  mockRecords(){
    wx.navigateTo({
        url: `/pages/mock_records/index?courseId=${this.data.user_info.selTypeId}`
    }); 
  },
  //收藏本
  collectBook:function(params) {
    wx.navigateTo({
        url: `/pages/collect_book/index?courseId=${this.data.user_info.selTypeId}`
    });
  },
  //常用查询
  searchShow(){
    this.setData({
        searchIsShow:true
    });
  },
  searchHide(){
    this.setData({
        searchIsShow:false
    });
  },
  searchItem(params){
      if(params.detail.index==0){
        //成绩/证书查询
       let url="https://bm.ruankao.org.cn/sign/welcome";
        wx.navigateTo({
            url: `/pages/web_view/index?url=${url}`
          });
      }else if(params.detail.index==1){
        //软考报考指南
      }else if(params.detail.index==2){
        //考试安排最新通知
      }
     
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    let user_info = wx.getStorageSync('web_user_info');
    if (user_info) {
      this.setData({
        isLogin: true,
        user_info: user_info
      });
    }else{
        this.setData({
            isLogin: false
        });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})